import { confirmPayment, payGoodsPrice } from '@/api';
export default {
  data() {
    return {
      payStatus: true,
      advanceType: null,
      paymentForm: {
        order_id: null,
        pay_type: null,
        password: null,
      },
    };
  },
  methods: {
    /**
     * 手动解析 "yyyy-MM-dd HH:mm:ss" 格式的日期字符串（兼容iOS）
     * @param {string} dateStr - 输入格式："2025-08-25 17:15:52"
     * @returns {Date|null} 解析后的Date对象（失败返回null）
     */
    parseIosCompatibleDate(dateStr) {
      // 校验格式是否匹配
      const reg = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
      const match = dateStr.match(reg);

      if (!match) {
        console.error("日期格式错误，需为 'yyyy-MM-dd HH:mm:ss'");
        return null;
      }

      // 提取年、月、日、时、分、秒（注意月份从0开始，需减1）
      const year = parseInt(match[1], 10);
      const month = parseInt(match[2], 10) - 1; // 月份0-11
      const day = parseInt(match[3], 10);
      const hour = parseInt(match[4], 10);
      const minute = parseInt(match[5], 10);
      const second = parseInt(match[6], 10);

      // 手动构造Date对象
      return new Date(year, month, day, hour, minute, second);
    },

    handleCopy(content) {
      uni.setClipboardData({
        data: content,
        success: () => {
          uni.showToast({
            title: '复制成功',
            icon: 'none',
            duration: 2000,
          });
        },
        fail: (err) => {
          uni.showToast({
            title: '复制失败',
            icon: 'none',
            duration: 2000,
          });
          console.error('复制失败原因：', err);
        },
      });
    },
    payTypeClose(value) {
      this.$nextTick(() => {
        if (this.payStatus) {
          if (!value.show)
            uni.redirectTo({
              url:
                '/pages-worker/result?type=error&id=' +
                this.paymentForm.order_id,
            });
        }
      });
    },
    // 打开支付类型列表
    payTypeClick(form, advanceType) {
      this.advanceType = advanceType;
      this.$set(this.paymentForm, 'order_id', form.id);
      this.$refs.payPopup.open();
      if (advanceType == 1) {
        this.$refs.orderPop.closeTap();
      }
      this.$nextTick(() => {
        if (advanceType == 1) {
          this.$refs.payVue.price = form.advance_payment_price;
        } else {
          this.$refs.payVue.price = form.order_price;
        }
      });
    },

    payClick(value) {
      this.payStatus = false;
      this.$refs.payPopup.close();
      if (value === 'money') {
        this.$set(this.paymentForm, 'pay_type', value);
        this.$refs.popPwdRef.open();
      }
    },

    confirmPaymentTap() {
      if (!this.paymentForm.password) return uni.$uv.toast('请输入密码');
      uni.showLoading({ title: '支付中', mask: true });
      if (this.advanceType == 1) {
        payGoodsPrice(this.paymentForm).then((res) => {
          uni.hideLoading();
          if (res.code === 200) {
            uni.$uv.toast('支付成功');
            this.$nextTick(() => {
              this.payOrderFun();
            });
          }
        });
        return;
      }
      confirmPayment(this.paymentForm).then((res) => {
        uni.hideLoading();
        if (res.code === 200) {
          uni.$uv.toast('支付成功');
          this.$nextTick(() => {
            this.payOrderFun();
          });
        }
      });
    },
  },
};
